package gui.client;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.ClientComplaint;

import javax.swing.JEditorPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import control.ComplaintController;
import control.LoginController;
/**
 * this class use to Create Complaint Screen
 * @author G8
 *
 */
public class CreateComplaintScreen extends JPanel implements ActionListener{
	
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSend;
	public JTextArea textAreaComplaintContent;
	private ComplaintController complaintController;
	/**
	 *Create Complaint Screen Constructor 
	 */
	public CreateComplaintScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "New Complaint", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		JLabel lblComplaint = new JLabel("Complaint:");
		lblComplaint.setBounds(69, 96, 92, 14);
		add(lblComplaint);
		
		textAreaComplaintContent = new JTextArea();
		JScrollPane scroll = new JScrollPane (textAreaComplaintContent, 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setBounds(69, 121, 365, 160);
		add(scroll);
		
		btnSend = new JButton("Send");
		btnSend.setBounds(180, 292, 131, 35);
		btnSend.addActionListener(this);
		add(btnSend);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSend){
			complaintController = new ComplaintController();
			String complaintMessage = new String("Complaint from "+VCP.currentUser.getUserFirstName()+" "+VCP.currentUser.getUserLastName()+
					"\n\n"+textAreaComplaintContent.getText()+"\n\nPlease reply within 24 hours.");
			ClientComplaint newComplaint = new ClientComplaint("COMPLAINT",complaintMessage,VCP.currentUser.getUserName());
			SystemTypes complaintToSend = new SystemTypes(newComplaint,null);
			SystemTypes complaintResult = complaintController.SendComplaint(complaintToSend);
			if (complaintResult.getSysMode().equals(SystemMode.OK)){
				JOptionPane.showMessageDialog(null, complaintResult.getObj().toString(), "Success",
	                    JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(null, complaintResult.getObj().toString(), "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
